package com.jiantong.work.impl.service.yangzhibaoxian;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.BankcardDao;

import com.jiantong.work.model.yangzhibaoxian.Bankcard;
import com.jiantong.work.service.yangzhibaoxian.BankcardService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/5.
 */
@Slf4j
@Service
@RpcProvider
public class BankcardServiceImpl implements BankcardService{

    private BankcardDao bankcardDao;

    @Autowired
    public BankcardServiceImpl(BankcardDao bankcardDao) {
        this.bankcardDao = bankcardDao;
    }

    @Override
    public Response<Bankcard> findById(Long id) {
        try {
            Bankcard bankcard = (Bankcard) bankcardDao.findById(id);
            if (bankcard == null) {
                log.error("bankcard(id={}) is not found", id);
                return Response.fail("bankcard.not.found");
            }

            return Response.ok(bankcard);
        } catch (Exception e) {
            log.error("failed to find bankcard(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("bankcard.find.fail");
        }
    }

    @Override
    public Response<List<Bankcard>> findByIds(List ids) {
        try {
            List<Bankcard> bankcardList=  bankcardDao.findByIds(ids);
            if (bankcardList == null) {
                log.error("immunizationRecord(ids={}) is not found", ids);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(bankcardList);
        } catch (Exception e) {
            log.error("failed to find bankcardList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("bankcardList.find.fail");
        }

    }

    @Override
    public Response<List<Bankcard>> list(Map map) {
        try {
            List<Bankcard> bankcardList=  bankcardDao.list(map);
            if (bankcardList == null) {
                log.error("immunizationRecord(ids={}) is not found", map);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(bankcardList);
        } catch (Exception e) {
            log.error("failed to find bankcardList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("bankcardList.find.fail");
        }
    }

    @Override
    public Response<Paging<Bankcard>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<Bankcard> bankcardList=  bankcardDao.paging(pageInfo.getOffset(), pageInfo.getLimit(),map);
            if (bankcardList == null) {
                log.error("immunizationRecord(ids={}) is not found", map);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(bankcardList);
        } catch (Exception e) {
            log.error("failed to find bankcardList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("bankcardList.find.fail");
        }
    }

    @Override
    public Boolean update(Bankcard bankcard) {
        try {
            Boolean b=bankcardDao.update(bankcard);
            return b;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Long create(Bankcard bankcard) {
        try {
            bankcardDao.create(bankcard);
            return bankcard.getId();
        }catch (Exception e){
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<Bankcard> bankcardList) {
        try {

            return  bankcardDao.creates(bankcardList);
        }catch (Exception e){
            e.printStackTrace();
            return -500;
        }
    }
}
